package com.opengroupe.adapter;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.opengroupe.R;
import com.opengroupe.activity.ConsulterResultatActivity;
import com.opengroupe.activity.ResultatActivity;
import com.opengroupe.dto.CandidatDto;

public class ConsulterResultatAdapter extends ArrayAdapter<CandidatDto>{
	
	private List<CandidatDto> candidatDtos;
    private Context context;
	private int layoutResourceId;
	private String TAG;
	
	public ConsulterResultatAdapter(Context context, int resourceLayoutId, List<CandidatDto> candidatDtos) {
		super(context, resourceLayoutId, candidatDtos);
		this.layoutResourceId = resourceLayoutId;
		this.context = context;
		this.candidatDtos = (ArrayList<CandidatDto>) candidatDtos;
		TAG = context.getString(R.string.log_prefix);
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		CandidatHolder holder;
		
		if (row == null) {
			LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            
            holder = new CandidatHolder();
            
            holder.txtNomCandidat = (TextView) row.findViewById(com.opengroupe.R.id.txtNomCandidat);
            holder.txtHiddenEmailCandidat = (TextView) row.findViewById(com.opengroupe.R.id.txtHiddenEmailCandidat);
            holder.itemCandidat = (LinearLayout) row.findViewById(com.opengroupe.R.id.itemCandidat);
            
            holder.txtNomCandidat.setTag(holder);
            holder.txtHiddenEmailCandidat.setTag(holder);
            row.setTag(holder);
            
		}else {
        	holder = (CandidatHolder)row.getTag();
        }
		
		CandidatDto candidatDto = candidatDtos.get(position);
		holder.txtNomCandidat.setText(candidatDto.getPrenomCandidat()+" "+candidatDto.getNomCandidat());
		holder.txtHiddenEmailCandidat.setText(candidatDto.getEmailCandidat());
		holder.itemCandidat.setOnClickListener(candidatListener);
		
		return row;
	}
	
	private OnClickListener candidatListener = new OnClickListener() {
		
		public void onClick(View v) {
			LinearLayout itemCandidat = (LinearLayout) v;
			CandidatHolder cand = (CandidatHolder)itemCandidat.getTag();
			
			Intent intent = new Intent(getContext(), ResultatActivity.class);
			if(intent.getExtras() != null){
				intent.getExtras().clear();
			}
			
			Log.d(TAG + " emailCandidat = ",cand.txtHiddenEmailCandidat.getText().toString());
			intent.putExtra("emailCandidat", cand.txtHiddenEmailCandidat.getText().toString());
			intent.putExtra("caller", ConsulterResultatActivity.class.getCanonicalName());
			context.startActivity(intent);
		}
	};
	
    static class CandidatHolder
    {
        TextView txtNomCandidat;
        TextView txtHiddenEmailCandidat;
        LinearLayout itemCandidat;
    }
}
